package com.tianbing.adlottery.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.fengniu.ejb.business.IContentBusiness;
import com.fengniu.ejb.business.ISiteBusiness;
import com.fengniu.ejb.business.IUserBusiness;
import com.fengniu.ejb.dao.ICategoryDao;
import com.fengniu.ejb.dao.IContentDao;
import com.fengniu.ejb.dao.ISiteDao;
import com.fengniu.ejb.dao.ISystemDao;
import com.fengniu.ejb.dao.IUserDao;
import com.fengniu.ejb.entity.Category;
import com.fengniu.ejb.entity.Content;
import com.fengniu.ejb.entity.Site2Content;
import com.tianbing.adlottery.global.IConstants;
import com.tianbing.adlottery.helper.SkinUtils;
import com.tianbing.adlottery.util.UtilDataGroup;

/**
 * MultiActionController for the image list/upload UI.
 * 
 * @author Conan Chen
 * @since 2006.12.15
 */
public class ContentController extends MultiActionController {
	Logger logger = Logger.getLogger(ContentController.class.getName());

	private String productDetailView;

	private String ajaxGetProductDetailView;

	private String ajaxGetProductStoreListView;

	private ISiteDao siteDao;

	private ISiteBusiness siteBusiness;

	private IUserDao userDao;

	private IContentDao contentDao;

	private ICategoryDao categoryDao;

	private SkinUtils skinUtils;

	private ISystemDao systemDao;

	private IUserBusiness userBusiness;

	private IContentBusiness contentBusiness;

	@SuppressWarnings("unchecked")
	public ModelAndView viewProductDetail(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mv = new ModelAndView(productDetailView);
		Map model = mv.getModel();

		// get request parameters sgid is Site-Group-Id
		String cgid = ServletRequestUtils.getStringParameter(request, "cgid", null);
		Long csid = ServletRequestUtils.getLongParameter(request, "csid", -1);
		String sgid = ServletRequestUtils.getStringParameter(request, "sgid", IConstants.CategoryID.AREA_TOP.getId());
		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = ServletRequestUtils.getIntParameter(request, "pgsize", 10);
		String odby = ServletRequestUtils.getStringParameter(request, "odby", "");

		// 1.get product detail
		Content contentDetail = contentDao.findContentBySysId(csid);
		model.put("contentDetail", contentDetail);
		model.put("sgid", sgid);

		// 5.1 Get stores sell this product
		Map s2cMap = siteBusiness.getStoreSellProducts(csid, -1, sgid, pgno, pgsize);
		List<Site2Content> site2ContentList = (List<Site2Content>) s2cMap.get("site2ContentList");
		Long totalCount = (Long) s2cMap.get("totalCount");

		model.put("pagefooterparas", "csid=" + csid + "&sgid=" + sgid);
		model = skinUtils.preparePageFooter(request, site2ContentList, totalCount, pgno, pgsize, model);
		model.put("referurl", request.getRequestURL().toString() + "?" + request.getQueryString());

		
		return mv;
	}

	@SuppressWarnings("unchecked")
	public ModelAndView ajaxGetProductDetail(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mv = new ModelAndView(ajaxGetProductDetailView);
		Map model = mv.getModel();

		// get request parameters sgid is Site-Group-Id
		String cgid = ServletRequestUtils.getStringParameter(request, "cgid", null);
		Long csid = ServletRequestUtils.getLongParameter(request, "csid", -1);
		String sgid = ServletRequestUtils.getStringParameter(request, "sgid", IConstants.CategoryID.AREA_TOP.getId());
		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = ServletRequestUtils.getIntParameter(request, "pgsize", 10);
		String odby = ServletRequestUtils.getStringParameter(request, "odby", "");

		// 1.get product detail
		Content contentDetail = contentDao.findContentBySysId(csid);
		model.put("contentDetail", contentDetail);

		// 2.get product's cateogry trail
		List<Category> primaryContentCategoryTrail = contentBusiness.getContentLongestCategoryTrail(csid,
				IConstants.CategoryID.CONTENT_TOP.getId());
		if (primaryContentCategoryTrail != null && primaryContentCategoryTrail.size() > 0) {
			model.put("primaryContentCategoryTrail", primaryContentCategoryTrail);
			Category contentCategory = primaryContentCategoryTrail.get(primaryContentCategoryTrail.size() - 1);
			model.put("contentCategory", contentCategory);
		}

		// 4.get recommended site 2 this product
		Long ssid = ServletRequestUtils.getLongParameter(request, "ssid", -1);
		if (ssid > -1 && csid > -1) {
			Site2Content recommendedSite2Content = siteBusiness.findStoreSellProduct(csid, ssid);
			model.put("recommendedSite2Content", recommendedSite2Content);
		}

		// 5.0 get site's cateogry trail
		List<Category> primarySiteCategoryTrail = categoryDao.getListCategoryPrimaryTrailBy(sgid);
		if (primarySiteCategoryTrail != null && primarySiteCategoryTrail.size() > 0) {
			model.put("primarySiteCategoryTrail", primarySiteCategoryTrail);
			Category siteCategory = primarySiteCategoryTrail.get(primarySiteCategoryTrail.size() - 1);
			if (siteCategory != null) {
				model.put("siteCategory", siteCategory);
				// get site child categories of siteCategory
				List<Category> siteChildCategoryList = categoryDao.getListChildCategoryBy(siteCategory.getCategoryId(),
						0, 100, null);
				List siteChildCategoryListGroups = UtilDataGroup.makeGroups(siteChildCategoryList, 0, 8);
				model.put("siteChildCategoryListGroups", siteChildCategoryListGroups);
			}
		}

		// 5.1 Get stores sell this product
		Map s2cMap = siteBusiness.getStoreSellProducts(csid, -1, sgid, pgno, pgsize);
		List<Site2Content> site2ContentList = (List<Site2Content>) s2cMap.get("site2ContentList");
		Long totalCount = (Long) s2cMap.get("totalCount");

		List site2ContentGroupList = UtilDataGroup.makeGroups(site2ContentList, 0, 6);
		model.put("pagefooterparas", "csid=" + csid + "&sgid=" + sgid);
		model = skinUtils.preparePageFooter(request, site2ContentList, totalCount, pgno, pgsize, model);
		model.put("referurl", request.getRequestURL().toString() + "?" + request.getQueryString());
		model.put("site2ContentGroupList", site2ContentGroupList);

		return mv;
	}

	public ModelAndView ajaxGetProductStoreList(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView mv = new ModelAndView(ajaxGetProductStoreListView);
		Map model = mv.getModel();

		// get request parameters sgid is Site-Group-Id
		String cgid = ServletRequestUtils.getStringParameter(request, "cgid", null);
		Long csid = ServletRequestUtils.getLongParameter(request, "csid", -1);
		String sgid = ServletRequestUtils.getStringParameter(request, "sgid", null);
		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = ServletRequestUtils.getIntParameter(request, "pgsize", 10);
		String odby = ServletRequestUtils.getStringParameter(request, "odby", "");

		// 5.1 Get stores sell this product
		Map s2cMap = siteBusiness.getStoreSellProducts(csid, -1, sgid, pgno, pgsize);
		List<Site2Content> site2ContentList = (List<Site2Content>) s2cMap.get("site2ContentList");
		Long totalCount = (Long) s2cMap.get("totalCount");

		List site2ContentGroupList = UtilDataGroup.makeGroups(site2ContentList, 0, 6);
		model.put("site2ContentGroupList", site2ContentGroupList);
		return mv;
	}

	public void setCategoryDao(ICategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}

	public void setProductDetailView(String homeView) {
		this.productDetailView = homeView;
	}

	public void setSiteDao(ISiteDao siteDao) {
		this.siteDao = siteDao;
	}

	public void setSkinUtils(SkinUtils skinUtils) {
		this.skinUtils = skinUtils;
	}

	public void setSystemDao(ISystemDao systemDao) {
		this.systemDao = systemDao;
	}

	public void setContentDao(IContentDao contentDao) {
		this.contentDao = contentDao;
	}

	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	public void setLogger(Logger logger) {
		this.logger = logger;
	}

	public void setSiteBusiness(ISiteBusiness siteBusiness) {
		this.siteBusiness = siteBusiness;
	}

	public void setUserBusiness(IUserBusiness userBusiness) {
		this.userBusiness = userBusiness;
	}

	public void setContentBusiness(IContentBusiness contentBusiness) {
		this.contentBusiness = contentBusiness;
	}

	public void setAjaxGetProductDetailView(String ajaxGetProductDetailView) {
		this.ajaxGetProductDetailView = ajaxGetProductDetailView;
	}

	/**
	 * @param ajaxGetProductStoreListView the ajaxGetProductStoreListView to set
	 */
	public void setAjaxGetProductStoreListView(String ajaxGetProductStoreListView) {
		this.ajaxGetProductStoreListView = ajaxGetProductStoreListView;
	}

}
